﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace Ks.Fs.Core.Common
{
    public class ThumbHelper
    {
        public static string[] ImageExtentions = new string[] { ".jpg", ".bmp", ".png", ".gif", ".jpeg" };

        public static bool IsExitsFolder(string path)
        {
            return Directory.Exists(path);
        }

        public static bool IsImageFile(string path)
        {
            string extention = Path.GetExtension(path);
            if (ImageExtentions.Contains(extention))
            {
                return true;
            }

            return false;
        }

        public static bool CreateFolder(string path)
        {
            var dir = Directory.CreateDirectory(path);
            if (dir != null) return true;
            return false;
        }

        /// <summary>
        /// Generate a thumb image with certance width and height
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="thumbFileName"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static bool GenThumbImage(string imageFile, string thumbFileName, int width, int height)
        {
            bool result = false;
            if (!IsImageFile(imageFile)) return false;
            using (FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read))
            {
                Image image = Image.FromStream(fs);
                Image thumb = image.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
                result = SaveImage(thumb, thumbFileName);
                image.Dispose();
                thumb.Dispose();
                fs.Close();
                fs.Dispose();
            }
            return result;
        }

        /// <summary>
        /// Generate a thumb image with certance with and auto scaled height
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="thumbFilename"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static bool GenThumbImage(string imageFile, string thumbFilename, int width)
        {
            bool result = false;
            try
            {
                if (!IsImageFile(imageFile)) return false;
                using (FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read))
                {
                    Image image = Image.FromStream(fs);
                    int ratio = image.Width / image.Height;
                    int height = width / ratio;
                    Image thumb = image.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
                    result = SaveImage(thumb, thumbFilename);
                    image.Dispose();
                    thumb.Dispose();
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Save a image
        /// </summary>
        /// <param name="image"></param>
        /// <param name="outputFile"></param>
        /// <returns></returns>
        static bool SaveImage(Image image, string outputFile)
        {
            using (var newImage = new Bitmap(image))
            {
                string folderPath = Path.GetDirectoryName(outputFile);
                if (!IsExitsFolder(folderPath))
                {
                    CreateFolder(folderPath);
                }
                newImage.Save(outputFile);
                newImage.Dispose();
            }
            return true;

        }
    }
}
